/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package network.packets;

import utils.StringUtils;
import utils.Utils;
/**
 *
 * @author Kvest
 */
public class TCPMessageResponce extends Packet
{
    public static final String CANCEL_ORDER_CLASS_NAME = "IMAP.Net.OrderIsNotYours_mesbindata";
    
    private String mesType;
    private String message;
    private String from;
    private String to;
    private long reciveDate;
    private String className;
    private int orderID;

    public TCPMessageResponce(byte[] data)
    {
        super(TCPMESSAGE_RESPONCE);
        mesType = "";
        message = "";
        from = "";
        to = "";
        reciveDate = 0;
        className = "";
        orderID = -1;
        
        parse(data);
    }

    protected void parse(byte[] data)
    {
        int offset = 0;

        //Пропускаем название пакета
        byte[] buffer4 = new byte[4];
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length + Utils.byteToInt(buffer4);
        
        //������ �������� - MesType
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        int strSize = Utils.byteToInt(buffer4);
        byte[] buffer = new byte[strSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        mesType = StringUtils.bytesToStr(buffer);
        
        //Message
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        strSize = Utils.byteToInt(buffer4);
        buffer = new byte[strSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        message = StringUtils.bytesToStr(buffer);

        //From
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        strSize = Utils.byteToInt(buffer4);
        buffer = new byte[strSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        from = StringUtils.bytesToStr(buffer);

        //To
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        strSize = Utils.byteToInt(buffer4);
        buffer = new byte[strSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        to = StringUtils.bytesToStr(buffer);

        //����� �������� - ���a
        buffer = new byte[8];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        reciveDate = Utils.byteToDate(buffer);

        //BinData
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        int size = Utils.byteToInt(buffer4);
        if (size > 0)
        {
            //className
            System.arraycopy(data, offset, buffer4, 0, buffer4.length);
            offset += buffer4.length;
            strSize = Utils.byteToInt(buffer4);
            buffer = new byte[strSize];
            System.arraycopy(data, offset, buffer, 0, buffer.length);
            offset += buffer.length;
            className = StringUtils.bytesToStr(buffer);

            //if (className.equals("IMAP.Net.OrderIsYours_mesbindata")||className.equals("IMAP.Net.OrderIsNotYours_mesbindata"))
            if (!className.equals(""))
            {
                System.arraycopy(data, offset, buffer4, 0, buffer4.length);
                offset += buffer4.length;
                orderID = Utils.byteToInt(buffer4);
            }
        }

        buffer4 = null;
        buffer = null;
    }

    public String getMesType()
    {
        return mesType;
    }

    public String getMessage()
    {
        return message;
    }

    public void setFrom(String value)
    {
        from = value;
    }

    public String getFrom()
    {
        return from;
    }

    public String getTo()
    {
        return to;
    }

    public long getReciveDate()
    {
        return reciveDate;
    }

    public String getClassName()
    {
        return className;
    }

    public int getOrderID()
    {
        return orderID;
    }
}
